<template>
	<view class="content">
		<!-- 搜索框 -->
		<view class="cu-bar bg-green search fixed" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="请输入产品名称" confirm-type="search" @input="handleSearch"></input>
			</view>
		</view>
		<mescroll-uni  top="100upx" class="my-mescroll" ref="mescrollRef" @init="mescrollInit" :down="downOption" :up="upOption" @down="downCallback" @up="upCallback">
		
		<view class="pro-list">
			<view class="item" v-for="(item, index) in dataList" :key="index" @click="showDetail(item)">
				<view class="title">{{item.name}}</view>
				<view class="price">产品价格：<text class="num">￥{{item.price}}</text></view>
				<view class="type">产品类型：{{item.cateName}}</view>
			</view>
			 
		</view>
		</mescroll-uni>
	</view>
</template>

<script>
	import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
	import api from '@/api/api.js';
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					auto: true, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				dataList: [],
				searchContent:'',
				
			}
		},
		onLoad() {
			// 修改顶部导航背景色
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: '#39b54a',
				animation: {
					duration: 400,
					timingFunc: 'easeIn'
				}
			})
		},
		methods: {
			// mescroll组件初始化的回调,可获取到mescroll对象
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				this.mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				console.log(page)
				this.getProductList(page);
			},
			getProductList(page){
				var data = {page:page.num,searchVal:this.searchContent};
				var that=this;
				api.getProductList(data).then(
					res => {
						if(res.code==200){
							console.log(page.num)
							console.log(res.data);
							console.log(res.data.size);
							console.log(res.data.total);
							
							that.mescroll.endBySize(res.data.size, res.data.total);
							//设置列表数据
							if (page.num ==1) {
								that.dataList = res.data.list; 
							} else {
								that.dataList = that.dataList.concat(res.data.list);
							}
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
							that.dataList = null;
							that.mescroll.endErr();
							// that.mescroll.endSuccess(0);
						}
					},
					rej => {}
				);
			},
			// 查看详情
			showDetail(item){
				uni.navigateTo({
					url: './productDetail?productId='+item.productId
				})
			},
			// 顶部搜索
			handleSearch(e) {
				let key = e.detail.value
				this.searchContent=key;
				this.upCallback({num:1,size:10})
			}
			 
		}
	}
</script>

<style scoped lang="scss">
	.content{
		.pro-list{
			background: #fff;
			.item{
				padding: 20upx;
				border-bottom: 1px solid #f7f7f7;
				&:last-child{
					border-bottom: none;
				}
				.title{
					font-size: 32upx;
					color: #333;
					margin-bottom: 10upx;
				}
				.price{
					font-size: 28upx;
					color: #737373;
					.num{
						font-size: 32upx;
						color: #E6A23C;
						font-weight: bold;
					}
				}
				.type{
					font-size: 28upx;
					color: #737373;
				}
			}
		}
	}
</style>
